
const pointprocess = require("../models/pointProcess")
// const userLearn = require("../models/userLearn")
const user = require("../models/userDetail")

var express = require('express');
var router = express.Router();

var formidable = require('formidable');

router.post('/', (req, res) => {
    var form = new formidable.IncomingForm();
    form.parse(req, async (err, fields) => {
        if(err) {
            res.json({'state': false, 'msg': err});
        }else {
            let date = new Date(fields.date)
            pointprocess.find({
                time: {
                    $gte: new Date(date.setHours(0, 0, 0, 0)).getTime(),
                    $lte: new Date(date.setHours(24, 0, 0, 0)).getTime()
                }
            }, {
              UUID: 1,
              time: 1
            }).distinct("UUID").exec((err, results) => {
                if(err) {
                    res.json({'state': false, 'msg': '查询每日浏览名单出错'});
                }else {

                    user.find({
                      UUID: {
                        $in: results
                      }
                    }, {
                      UUID: 1,
                      email: 1,
                      realName: 1,
                      school: 1,
                      stuNo: 1,
                      username: 1
                    }, (err, doc) => {
                      if(err){
                        res.json({'state': false, 'msg': '查询每日浏览用户信息出错'});
                      } else {
                          res.json({
                              'state': true,
                              'date': new Date(date.setHours(0, 0, 0, 0)).getTime(),
                              'results': doc
                          });
                      }
                    })
                  
                    
                }
            })
        }
    });
})

module.exports = router;